import { PageDto } from "../../models/page.model";
import { Tweet } from "../../models/tweet.model";
import { UaVideoEntity, VideoTypeMap } from "../../models/ua-card.model";
import { UaGinryuTimelineEntity, TimelineType, TimelineTypeListLabelMap, TimelineTypeListColorMap, TimelineTypeListBgColorMap } from "../../models/ua-timeline-item.entity";
import { GlobalService } from "../../services/global.service";
import { UaInfoService } from "../../services/ua-info.service";
import { UaTimelineService } from "../../services/ua-timeline.service";
import { UaVideoService } from "../../services/ua-video.service";
import { formatTime } from "../../utils/util";

enum InfoType {
  Daily = 'Daily',
  Video = 'Video',
  Timeline = 'Timeline'
}

// pages/info/info.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tweets: [] as Tweet[],
    videos: [] as UaVideoEntity[],
    TIMELINE: GlobalService.STATIC_FILE_HOST + '/marketing/timeline-sm.png',
    DAILY: GlobalService.STATIC_FILE_HOST + '/marketing/daily-sm.png',
    VIDEO: GlobalService.STATIC_FILE_HOST + '/marketing/video-sm.png',
    InfoType: InfoType,
    currentType: InfoType.Daily,
    CDN_HOST: GlobalService.CDN_HOST,
    VideoTypeMap: VideoTypeMap,
    timelineList: [] as UaGinryuTimelineEntity[],
    TimelineType: TimelineType,
    TimelineTypeListLabelMap: TimelineTypeListLabelMap,
    TimelineTypeListColorMap: TimelineTypeListColorMap,
    TimelineTypeListBgColorMap: TimelineTypeListBgColorMap,
    GINRYU: GlobalService.STATIC_FILE_HOST + '/marketing/ginryu.png',
  },

  customData: {
    page: 1,
    totalPage: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad(options: any) {
    const currentType = options['type'];
    wx.setNavigationBarTitle({
      title: '最新资讯'
    });
    this.getTabBar().setData({
      value: '/pages/info/info'
    });
    if (currentType) {
      this.setData({
        currentType
      })
    }
    UaTimelineService.getALl().then((response: UaGinryuTimelineEntity[]) => {
      for (const item of response) {
        if (item.title?.includes('UC')) {
          item.chinese = true;
        }
      }
      this.setData({
        timelineList: response
      })
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.customData.page = 1;
    this.load();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.customData.page = 1;
    this.load();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.customData.page + 1 > this.customData.totalPage) {
      return;
    }
    this.customData.page++;
    this.load();
  },

  load() {
    if (this.data.currentType === InfoType.Daily) {
      UaInfoService.searchWithPage(this.customData.page).then((response: PageDto<Tweet>) => {
        this.customData.totalPage = response.meta!.pageCount!;
        for (const item of response.data ?? []) {
          item.updateDateStr = formatTime(new Date(item.updateDate!));
          item.images = (JSON.parse(item.cosKeys!) as string[]).map(url => GlobalService.CDN_HOST + url);
        }
        if (this.customData.page === 1) {
          this.setData({
            tweets: response.data
          })
        } else {
          this.setData({
            tweets: [...this.data.tweets, ...response.data!]
          })
        }
      });
    } else if (this.data.currentType === InfoType.Video) {
      UaVideoService.searchWithPage({}, this.customData.page).then((response: PageDto<UaVideoEntity>) => {
        this.customData.totalPage = response.meta!.pageCount!;
        for (const item of response.data ?? []) {
          item.uploadDateStr = formatTime(new Date(item.uploadDate!));
        }
        if (this.customData.page === 1) {
          this.setData({
            videos: response.data
          })
        } else {
          this.setData({
            videos: [...this.data.videos, ...response.data!]
          })
        }
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  view(e: any) {
    const tweets = this.data.tweets[e.currentTarget.dataset.idx]
    wx.previewImage({
      current: tweets.images![0], // 当前显示图片的http链接
      urls: tweets.images! // 需要预览的图片http链接列表
    })
  },

  timeline() {
    wx.navigateTo({
      url: '/pages/info/timeline/timeline.page'
    })
  },

  changeMode(e: any) {
    this.setData({ currentType: e.currentTarget.dataset['type'] })
    this.customData.page = 1;
    this.load();
  },

  video(e: any) {
    const video = this.data.videos[e.currentTarget.dataset.idx];
    UaVideoService.view(video.id!).then();
    wx.navigateToMiniProgram({
      appId: 'wx7564fd5313d24844',
      path: `pages/video/video?avid=${video.aid}`
    });
  },

  toBili(e: any) {
    const video = this.data.videos[e.currentTarget.dataset.idx];
    UaVideoService.hot(video.id!).then();
    wx.showToast({
      title: '已复制链接',
      icon: 'success',
      duration: 2000
    })
  },

  copy(e: any) {
    const video = this.data.videos[e.currentTarget.dataset.idx];
  },

  showProduction(e: any) {
    const index = e.currentTarget.dataset.idx;
    const data = this.data.timelineList[e.currentTarget.dataset.idx];
    data.showInfo = !data.showInfo;
    this.setData({
      [`timelineList[${index}]`]: data
    })
  }


})